<?php

require_once 'base.php';

function unsCalendar_getAdminSectionMenus(&$url, &$text) {
    static $j = 0;

    if ($j == 0) {
        $text = 'UNS Calendar';
        $url = $GLOBALS['babAddonUrl'] . "admin";
        $j++;
        return true;
    }

    return false;
}

function unsCalendar_getUserSectionMenus(&$url, &$text) {
    static $j = 0;
    if ($j == 0) {
        $text = 'UNS Calendar';
        $url = $GLOBALS['babAddonUrl'] . "user";
        $j++;
        return true;
    }
    return false;
}

function unsCalendar_onSectionCreate(&$title, &$content) {
    return false;
}

function unsCalendar_upgrade($version_base, $version_ini) {

    global $babDB;

    include_once $GLOBALS['babInstallPath'] . "utilit/devtools.php";



    /* CREATION DES TABLES */
    $addon = bab_getAddonInfosInstance('UNSCalendar');
    $tables = new bab_synchronizeSql($addon->getPhpPath() . 'dump.sql');

    if ($tables->isCreatedTable('unsCalendar_lectureTypes')) {
        $babDB->db_query("INSERT INTO `unsCalendar_lectureTypes` VALUES (null, 'Cours Magistral', 'CM'),(null, 'Travaux Dirigés', 'TD'),(null, 'Travaux Pratiques', 'TP');");
    }
    if ($tables->isCreatedTable('unsCalendar_buildings')) {
        $babDB->db_query("INSERT INTO `unsCalendar_buildings` VALUES (null, 'Batiement QLIO');");
    }
    $babDB->db_query("DROP VIEW IF EXISTS `unsCalendar_view_workPraticeGroups`;");
    $babDB->db_query("
        CREATE VIEW `unsCalendar_view_workPraticeGroups`
        AS
        SELECT
`unsDepartement_formations`.`id` as `formation_id`,
`formation_name`,
`formation_coordinatorID`,
`unsDepartement_tutorialGroups`.`id` as `tutorialGroup_id`,
`tutorialGroup_name`,
`unsDepartement_workPraticeGroups`.`id` as `workPraticeGroup_id`,
`workPraticeGroup_name`
FROM `unsDepartement_formations`, `unsDepartement_tutorialGroups`, `unsDepartement_workPraticeGroups`
WHERE `tutorialGroup_formationID` = `unsDepartement_formations`.`id`
AND `workPraticeGroup_tutorialGroupID` = `unsDepartement_tutorialGroups`.`id`
ORDER BY `formation_name`, `tutorialGroup_name`, `workPraticeGroup_name`
        ");

    return true;
}

function unsCalendar_onDeleteAddon() {
    return true;
}

function unsCalendar_onPageRefreshed() {
    $jquery = bab_functionality::get('jquery');
    if ($jquery !== false) {
        $jquery->includeCore();
        $jquery->includeUI();
    }
}

?>